package com.api.douyin.util;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 抖音API工具类
 */
public class DouYinApi {

    public final static int TIMEOUT = 10000;

    /**
     * 获得令牌
     * @return
     */
    public static String getToken() {
        String client_key = "awdhwtrv28chvdx1";
        String client_secret = "60a6e6bb15fb60caf5e0b0ee09345ede";
        String grant_type = "authorization_code";
        String code = "ef36ad85337d9fb7NJdw6RuvAdokjdWM4cQr";
        //接口地址
        String url = "https://open.douyin.com/oauth/access_token/";

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("client_key", client_key);
        param.put("client_secret", client_secret);
        param.put("grant_type", grant_type);
        param.put("code", code);

        String paramString = "client_key=" + client_key + "&client_secret=" + client_secret + "&grant_type=" + grant_type
                + "&code=" + code;

        HttpResponse response = HttpRequest.post(url).header(Header.CONTENT_TYPE, "multipart/form-data")
                        .form(param).timeout(TIMEOUT).execute();

        return response.body();
    }

    /**
     * 获得用户粉丝列表
     * @param openId
     * @param cursor
     * @param count
     * @param accessToken
     * @return
     */
    public static String getFans(String openId, Integer cursor, Integer count, String accessToken) {

        //接口地址
        String url = "https://open.douyin.com/fans/list/";

        Map<String, Object> param = new HashMap<>();
        param.put("open_id", openId);
        param.put("cursor", cursor);
        param.put("count", count);
        param.put("access_token", accessToken);

        HttpResponse response = HttpRequest.get(url).form(param).timeout(TIMEOUT).execute();

        return response.body();
    }
}
